#include "OgreApplication.h"
#include "Constants.h"

using namespace spiderConst;

OgreApplication::OgreApplication() 
: StandardApplication()
{
	_spiderMngr = SpiderManager::getSingletonPtr();
}

//------------------------------------------------------

OgreApplication::~OgreApplication()
{
}

//------------------------------------------------------

void OgreApplication::createCamera()
{
	// Create the camera
	_camera = _sceneManager->createCamera("PlayerCam");
	_camera->setPosition(Vector3(10,10,10));
	_camera->lookAt(0,0,0);
	// Look back along -Z
	_camera->setNearClipDistance(0.1);
}

//------------------------------------------------------

bool OgreApplication::configure()
{
	if(_root->showConfigDialog())
	{
		// If returned true, user clicked OK so initialise
		// Here we choose to let the system create a default rendering window by passing 'true'
		_window = _root->initialise(true);
		// Let's add a nice window icon
	#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		HWND hwnd;
		_window->getCustomAttribute("WINDOW", (void*)&hwnd);
		LONG iconID   = (LONG)LoadIcon( GetModuleHandle(0), MAKEINTRESOURCE(IDI_APPICON) );
		SetClassLong( hwnd, GCL_HICON, iconID );
	#endif
					return true;
	}
	else return false;

}

//------------------------------------------------------

bool OgreApplication::setup()
{
	if(!StandardApplication::setup()) return false;
	return true;
}
void OgreApplication::createScene()
{
 	//creating tarantula
	Entity* _entity = _sceneManager->createEntity("tarantula", "tarantula.mesh");
	_spiderMngr->setSpiderEntity(_entity);

	SceneNode* _node = _sceneManager->getRootSceneNode()->createChildSceneNode("tarantulaNode");
	_node->setPosition(0,0,0);
	_node->attachObject(_entity);
	_spiderMngr->setSpiderNode(_node);

	
	//==========TEMPORARLY==========
	//_spiderMngr->bone1	= _entity->getSkeleton()->getBone("leg_left_1_1");
	//_spiderMngr->bone2	= _entity->getSkeleton()->getBone("leg_left_1_2");
	//_spiderMngr->bone3	= _entity->getSkeleton()->getBone("leg_left_1_3");
	
	//_spiderMngr->bone1->setManuallyControlled(true);
	//_spiderMngr->bone2->setManuallyControlled(true);
	//_spiderMngr->bone3->setManuallyControlled(true);
	//==========TEMPORARLY-END==========

	Skeleton* _skeleton = _entity->getSkeleton();
	Ogre::Skeleton::BoneIterator _bIter = _skeleton->getBoneIterator();

	spiderBoneList& _bList = _spiderMngr->getSpiderBonesList();
	
	
	while(_bIter.hasMoreElements())
	{
		Ogre::Bone* _bone = _bIter.getNext();
		_bList[_bone->getName()] = _bone;
		_bone->setManuallyControlled(true);
	}
	_spiderMngr->createBoneSets();

	//creating plane
//	_entity = _sceneManager->createEntity("plane", "plane.mesh");
//	_entity->setMaterialName("Examples/Rockwall");
//	_node = _sceneManager->getRootSceneNode()->createChildSceneNode("planeNode");
//	_node->setScale(Vector3(10,1,10));
//	_node->attachObject(_entity);
//	_node->setPosition(0,-5,0);
	 Plane plane(Vector3::UNIT_Y, 0);
	 MeshManager::getSingleton().createPlane("ground", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500,1500,20,20,true,1,10,10,Vector3::UNIT_Z);
	 _entity = _sceneManager->createEntity("GroundEntity", "ground");
     _node = _sceneManager->getRootSceneNode()->createChildSceneNode();
	 _node->attachObject(_entity);
	 _entity->setMaterialName("Examples/Rockwall");
	 _node->setPosition(Vector3(0,-2,0));

	_entity->getSkeleton();	
}

//------------------------------------------------------

void OgreApplication::createFrameListener()
{
	_frameListener = new OgreApplicationFrameListener(_sceneManager, _window, _camera);
	_root->addFrameListener(_frameListener);
}

//------------------------------------------------------

void OgreApplication::createLights()
{
	_sceneManager->setAmbientLight(Ogre::ColourValue(.3,.3,.3));

	Light* _light = _sceneManager->createLight("light1");
	_light->setPosition(0,10,5);
	_light->setDiffuseColour(1,1,1);
	//_light->setAttenuation(0.1,0.5,.2,0);
	_light->setType(Ogre::Light::LT_POINT);
}
